<?php
// This page is used to retrieve url information from alexa server


// Format example:
// calculate_score.php?q=usc&response_group=SiteData|Speed|TrafficData|LinksInCount|Related


require_once("alexa_info.php");
require_once("google_search.php");


//response group
$response_group = $_GET['response_group'];

// determine request type
// url array need to be retrieve 	
$url_array = google_search($_GET['q']);
$final_result = array();
$index=0;

foreach($url_array as $url){
	$alexa = new alexa_info($url['url']);
	$alexa->make_URL_info_request("UrlInfo",$response_group);
	$url_info = $alexa->get_result_array();	
	array_push($final_result,array("id"=>$index,"url"=>urlencode($url['url']),"info"=>$url_info));
	$index++;
}
//print_r($final_result);
$final_result = calculate_score($final_result, $response_group);
	
// ajax send back
echo json_encode($final_result);
	
	
	
	
	
function calculate_score($arr,$response_group){
	$final_result = array();
	switch($response_group){
		case "Speed":
			$final_result = speed_score($arr);
			break;
		case "TrafficData":
			$final_result = traffic_score($arr);
			break;
		case "LinksInCount":
			$final_result = popularity_score($arr);
			break;
		case "SiteData":
			$final_result =  time_score($arr);
			break;
		default: break;
	}
	
	return array("result"=>$final_result);
}

function speed_score($arr){
	$min_load = $max_load = $min_percentile = $max_percentile = 0;

	foreach($arr as $url){
		if(isset($url['info']['MedianLoadTime']) && $url['info']['MedianLoadTime']!="")
			$load = $url['info']['MedianLoadTime'];
		else $load = $url['info']['MedianLoadTime'] = 0;
		
		if(isset($url['info']['Percentile']) && $url['info']['Percentile']!="")
			$percentile = $url['info']['Percentile'];
		else $percentile = $url['info']['Percentile'] = 0;
		
		if($url['id'] == 0){
			$min_load = $max_load = $load;
			$min_percentile = $max_percentile = $percentile;
		}
		else{
			if($load<$min_load) $min_load = $load;
			if($load>$max_load) $max_load = $load;
			if($percentile<$min_percentile) $min_percentile = $percentile;
			if($percentile>$max_percentile)	$max_percentile = $percentile;
		}
	}
	$final_score = array();
	foreach($arr as $url){
		$load = $url['info']['MedianLoadTime'];
		$percentile = $url['info']['Percentile'];
		
		if($max_load == $min_load) $l = 1;
		else $l = ($max_load - $load)/($max_load - $min_load);
		if($max_percentile == $min_percentile) $p = 1;
		else $p = ($percentile-$min_percentile)/($max_percentile - $min_percentile);
		
		$score = 0.5 * $l
			   + 0.5 * $p;
	
		array_push($final_score, $score);
	}
	return $final_score;	
}


function traffic_score($arr){
	$final_result = array();
	for($i=0; $i<count($arr[0]['info']['Usage']);$i++){
		array_push($final_result, single_traffic_score($arr,$i));
	}
	return $final_result;
}


function single_traffic_score($arr,$i){
	$min_bound = 0;
	$max_bound = 0;
	$pageview_min = 0;
	$pageview_max = 0;
	$traffics = array();
	$quality_score = array();
	$traffic_score = array();
	
	foreach($arr as $url){
		$reach = 0;
		$pageview = 0;
		$usage =  $url['info']['Usage'][$i];
		if($usage['Reach']['Value'] !="NaN"){
			$reach = $usage['Reach']['Value'];
		}
		else $reach = 0;
			
		if($usage['PageViews']['Value'] !="NaN"){
			$pageview = $usage['PageViews']['Value'];
		}
		else $pageview = 0;
		$id = $url['id'];
		$traffics[$id] = $reach * $pageview;
		
		if($id==0){
			$min_bound = $traffics[$id];
			$max_bound = $traffics[$id]; 
			$pageview_min = $pageview;
			$pageview_max = $pageview;
		}
		else{
			if($traffics[$id]!=0 && $traffics[$id] - $min_bound<0){
				$min_bound = $traffics[$id];
			}
			if($traffics[$id]>$max_bound) $max_bound = $traffics[$id];
			
			if($pageview!=0 && $pageview - $pageview_min<0)
				$pageview_min = $pageview;
			if($pageview - $pageview_max>0)
				$pageview_max = $pageview; 
		}
	
	}	
	
	foreach($arr as $url){
		$usage =  $url['info']['Usage'][$i];
		$id = $url['id'];
		if($max_bound - $min_bound == 0) 
			$traffic_score[$id] = 1;
		else if($traffics[$id]==0)
			$traffic_score[$id] = 0.5;
		else
			$traffic_score[$id] = ($traffics[$id] - $min_bound)/($max_bound - $min_bound);
			
		if($pageview_max - $pageview_min != 0){
			$temp = $usage['PageViews']['Value'];
			if($temp!="NaN")
				$quality_score[$id] = ($temp - $pageview_min)/ ($pageview_max - $pageview_min);	
			else $quality_score[$id] = 0.5;	
		}
		else{
			$quality_score[$id] = 1;
		}
	}
	return array("traffic_score"=>$traffic_score, "quality_score"=>$quality_score);	
}


function popularity_score($arr){
	$min = $max = 0;
	$final_result = array();
	foreach($arr as $url){
		$id= $url['id'];
		$count = $url['info']['LinksInCount'];
		if($id==0){
			$min = $max = $count;
		}
		else{
			if($count<$min) $min=$count;
			if($count>$max) $max=$count;
		}
	}
	
	foreach($arr as $url){
		if($max-$min == 0) array_push($final_result, 1);
		else{
			$count = $url['info']['LinksInCount'];
			array_push($final_result, ($count-$min)/($max-$min));
		}
	}
	
	return $final_result;
}


function time_score($arr){
	$min = $max = 0;
	$final_reuslt = array();
	foreach($arr as $url){
		$date = strtotime($url['info']['OnlineSince']);
		$id = $url['id'];
		
		if($id == 0){
			$min = $max = $date;
		}
		else{
			if($date<$min) $min =$date;
			if($date>$max) $max = $date;
		}	 
	}
	
	foreach($arr as $url){
		$date = strtotime($url['info']['OnlineSince']);
		if($max-$min==0) $final_result[$url['id']] = 1;
		else{
			$final_result[$url['id']] = ($date-$min)/($max-$min);
		}
	}
	
	return $final_result;
}

?>